import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";

import { registerMicroApps, start } from "qiankun";
import actions from './actions'

import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

// 解决不同路由跳转undefined问题
import _ from "lodash"
router.beforeEach((to, from, next) => {
  if (_.isEmpty(history.state.current)) {
    _.assign(history.state, { current: from.fullPath });
  }
  next();
});

const apps = [
  {
    name: "vue2", //子应用的名称
    entry: process.env.VUE_APP_SUB_VUE2, //子应用的域名
    container: "#vue2", //承载子应用的容器，在上面App.vue中定义
    activeRule: "/vue2", // 被激活的子应用的路由
    props: {
      baseUrl: '/vue2',
      getGlobalState: actions.getGlobalState
    }
  },
  {
    name: "vue3",
    entry: process.env.VUE_APP_SUB_VUE3,
    container: "#vue3",
    activeRule: "/vue3",
    props: {
      baseUrl: '/vue3',
      getGlobalState: actions.getGlobalState
    }
  },
  {
    name: "react",
    entry: process.env.VUE_APP_SUB_REACT,
    container: "#react",
    activeRule: "/react",
    props: {
      baseUrl: '/react',
      getGlobalState: actions.getGlobalState
    }
  },
  {
    name: "htm",
    entry: process.env.VUE_APP_SUB_HTM,
    container: "#htm",
    activeRule: "/htm",
    props: {
      baseUrl: '/htm',
      getGlobalState: actions.getGlobalState
    }
  },
];

registerMicroApps(apps); //注册子应用
start({
  excludeAssetFilter: (assetUrl) => {
    const whiteList = ['https://hm.baidu.com/hm.js']
    const whiteWords = ['_custom-exclude_=MAIN'] // 白名单关键词：协议制定 _custom-exclude_=MAIN。
    if (whiteList.includes(assetUrl)) {
      return true
    }
    return whiteWords.some(w => {
      return assetUrl.includes(w)
    })
  },
  sandbox: { prefetch: true, experimentalStyleIsolation: true, strictStyleIsolation: true } // 开启沙箱模式，并进行css隔离 // { strictStyleIsolation: true }
}); //启动qiankun

createApp(App).use(router).use(ElementPlus).mount("#app");
